package com.atguigu.jxc.service.impl;

import com.atguigu.jxc.dao.GoodsDao;
import com.atguigu.jxc.domain.ErrorCode;
import com.atguigu.jxc.domain.ServiceVO;
import com.atguigu.jxc.domain.SuccessCode;
import com.atguigu.jxc.entity.*;
import com.atguigu.jxc.service.CustomerReturnListGoodsService;
import com.atguigu.jxc.service.GoodsService;
import com.atguigu.jxc.service.LogService;
import com.atguigu.jxc.service.SaleListGoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @description
 */
@Service
public class GoodsServiceImpl implements GoodsService {

    @Autowired
    private GoodsDao goodsDao;

    @Override
    public ServiceVO getCode() {

        // 获取当前商品最大编码
        String code = goodsDao.getMaxCode();

        // 在现有编码上加1
        Integer intCode = Integer.parseInt(code) + 1;

        // 将编码重新格式化为4位数字符串形式
        String unitCode = intCode.toString();

        for(int i = 4;i > intCode.toString().length();i--){

            unitCode = "0"+unitCode;

        }
        return new ServiceVO<>(SuccessCode.SUCCESS_CODE, SuccessCode.SUCCESS_MESS, unitCode);
    }

    @Override
    public Map<String, Object> list(Integer page, Integer rows, String codeOrName, Integer goodsTypeId) {
        Map<String,Object> map = new HashMap<>();
        page = page == 0 ? 1 : page;
        int offSet = (page - 1) * rows;

        if (goodsTypeId != null){
            map.put("rows",goodsDao.getGoodsListByGoodsTypeId(goodsTypeId));
        }else{
            List<Goods> list = goodsDao.getGoodsList(offSet,rows,codeOrName);
            map.put("rows",list);
        }
        map.put("total",goodsDao.getGoodsCount(codeOrName,goodsTypeId));
        return map;
    }



    //查询所有商品单位
    @Override
    public Map<String, Object> getGoodsUnit() {
        Map<String,Object> map = new HashMap<>();
        List<Unit> units = goodsDao.getGoodsUnit();
        map.put("rows",units);
        return map;
    }

    //删除商品
    @Override
    public void deleteGoodsType(Integer goodsTypeId) {

        goodsDao.deleteGoodsType(goodsTypeId);

    }

    @Override
    public List<GoodsTypeResponseVo> listGoodsType() {

        List<GoodsTypeResponseVo> responseVos =goodsDao.getGoodsTypeName();
        loop(responseVos);
        return responseVos;
    }

    private void loop(List<GoodsTypeResponseVo> responseVos) {
        for (GoodsTypeResponseVo responseVo : responseVos) {
            List<GoodsTypeResponseVo> childrenVos = null;
            Attributes attributes = new Attributes();
            if("closed".equals(responseVo.getState())){
                attributes.setState(1);
                childrenVos = this.packChildren(responseVo.getId());
                responseVo.setChildren(childrenVos);
            }else {
                attributes.setState(0);
            }
            responseVo.setAttributes(attributes);
        }
    }

    /**
     * 封装子集合
     * @param id
     * @return
     */
    private List<GoodsTypeResponseVo> packChildren(Integer id) {
        List<GoodsTypeResponseVo> childRenVos =goodsDao.packChildren(id);
        loop(childRenVos);
        return childRenVos;
    }


}
